package cn.iocoder.yudao.module.famous.doctor.controller.admin.inquiry;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.famous.doctor.controller.admin.inquiry.vo.*;
import cn.iocoder.yudao.module.famous.doctor.dal.dataobject.inquiry.InquiryDO;
import cn.iocoder.yudao.module.famous.doctor.convert.inquiry.InquiryConvert;
import cn.iocoder.yudao.module.famous.doctor.service.inquiry.InquiryService;

@Tag(name = "管理后台 - 我的问诊")
@RestController
@RequestMapping("/famous.doctor/inquiry")
@Validated
public class InquiryController {

    @Resource
    private InquiryService inquiryService;

    @PostMapping("/create")
    @Operation(summary = "创建我的问诊")
    @PreAuthorize("@ss.hasPermission('famous.doctor:inquiry:create')")
    public CommonResult<Long> createInquiry(@Valid @RequestBody InquiryCreateReqVO createReqVO) {
        return success(inquiryService.createInquiry(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新我的问诊")
    @PreAuthorize("@ss.hasPermission('famous.doctor:inquiry:update')")
    public CommonResult<Boolean> updateInquiry(@Valid @RequestBody InquiryUpdateReqVO updateReqVO) {
        inquiryService.updateInquiry(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除我的问诊")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('famous.doctor:inquiry:delete')")
    public CommonResult<Boolean> deleteInquiry(@RequestParam("id") Long id) {
        inquiryService.deleteInquiry(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得我的问诊")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('famous.doctor:inquiry:query')")
    public CommonResult<InquiryRespVO> getInquiry(@RequestParam("id") Long id) {
        InquiryDO inquiry = inquiryService.getInquiry(id);
        return success(InquiryConvert.INSTANCE.convert(inquiry));
    }

    @GetMapping("/list")
    @Operation(summary = "获得我的问诊列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('famous.doctor:inquiry:query')")
    public CommonResult<List<InquiryRespVO>> getInquiryList(@RequestParam("ids") Collection<Long> ids) {
        List<InquiryDO> list = inquiryService.getInquiryList(ids);
        return success(InquiryConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得我的问诊分页")
    @PreAuthorize("@ss.hasPermission('famous.doctor:inquiry:query')")
    public CommonResult<PageResult<InquiryRespVO>> getInquiryPage(@Valid InquiryPageReqVO pageVO) {
        PageResult<InquiryDO> pageResult = inquiryService.getInquiryPage(pageVO);
        return success(InquiryConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出我的问诊 Excel")
    @PreAuthorize("@ss.hasPermission('famous.doctor:inquiry:export')")
    @OperateLog(type = EXPORT)
    public void exportInquiryExcel(@Valid InquiryExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<InquiryDO> list = inquiryService.getInquiryList(exportReqVO);
        // 导出 Excel
        List<InquiryExcelVO> datas = InquiryConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "我的问诊.xls", "数据", InquiryExcelVO.class, datas);
    }

}
